/**
 *
 */
package org.yabam.dao.impl.fake;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.yabam.dao.DefaultDAO;
import org.yabam.model.Category;

/**
 * @author brlau69n
 *
 */
public class CategoryDAOImpl implements DefaultDAO<Category> {

	private static final CategoryDAOImpl INSTANCE = new CategoryDAOImpl();

	private CategoryDAOImpl() {
	}

	public static final CategoryDAOImpl getInstance() {
		return INSTANCE;
	}

	private static Map<Long, Category> categories = new HashMap<Long, Category>();
	static {
		Category cat1 = new Category(1, "cat1", null);
		categories.put(cat1.getId(), cat1);

		Category cat2 = new Category(2, "cat2", cat1);
		categories.put(cat2.getId(), cat2);

		Category cat3 = new Category(3, "cat3", null);
		categories.put(cat3.getId(), cat3);
	}

	/**
	 * @see org.yabam.dao.DefaultDAO#delete(java.lang.Object)
	 */
	@Override
	public void delete(Category toBeDeleted) {
		categories.remove(toBeDeleted.getId());
	}

	/**
	 * @see org.yabam.dao.DefaultDAO#get(java.lang.Object)
	 */
	@Override
	public Category get(Object id) {
		return categories.get(id);
	}

	/**
	 * @see org.yabam.dao.DefaultDAO#getAll()
	 */
	@Override
	public List<Category> getAll() {
		return new ArrayList<Category>(categories.values());
	}

	/**
	 * @see org.yabam.dao.DefaultDAO#saveOrUpdate(java.lang.Object)
	 */
	@Override
	public void saveOrUpdate(Category toBeSaved) {
		if (toBeSaved.getId() == 0) {
			toBeSaved.setId(Collections.max(categories.keySet()) + 1L);
		}
		categories.put(toBeSaved.getId(), toBeSaved);
	}

}
